package net.chathu.airgw.util 
{
	import flash.utils.ByteArray;
	import net.chathu.airgw.Config;
	/**
	 * ...
	 * @author ze
	 */
	public class StringUtil 
	{
		
		public static function convertFromISO88591(str:String):String {
			var bytearray:ByteArray = new ByteArray();
			bytearray.writeMultiByte(str, "ISO-8859-1");
			bytearray.position = 0;
			var ret:String = bytearray.readMultiByte(bytearray.bytesAvailable, "ISO-8859-2");
			return ret;
		}
				
		
		public static function convertFromISO88591ToUTF(str:String):String {
			var bytearray:ByteArray = new ByteArray();
			bytearray.writeMultiByte(str, "ISO-8859-1");
			bytearray.position = 0;
			trace("len: " + bytearray.bytesAvailable);
			var ret:String = bytearray.readMultiByte(bytearray.bytesAvailable, "UTF-8");
			return ret;
		}
		
		public static function UnicodeToUtf8( wstr:ByteArray):ByteArray
		{
			// Convert unicode to utf8
			var len:int = 0;

			wstr.position = 0;
			var wstrlen:int = wstr.bytesAvailable;
			trace("wstrlen: " + wstrlen);
			var w:int,b:int;
			for (w = 0; w < wstrlen; w++)
			{
				trace("ok: " + w);
				b = wstr.readUnsignedByte();
				if (b < 0x0080) len++;
				else if ( b<0x0800 ) len += 2;
				else len += 3;
			}
			
			var szOut:ByteArray = new ByteArray();
			
			wstr.position = 0;
			for (w = 0; w < wstrlen; w++)
			{
				trace("xok: " + w);
				b = wstr.readUnsignedByte();
				if (b < 0x0080)
				{
					szOut.writeByte(b);
				} else if ( b < 0x0800 ) {
					szOut.writeByte( 0xc0 | (b >> 6 ));
					szOut.writeByte( 0x80 | (b & 0x3f ));
				} else {
					szOut.writeByte( 0xe0 | ( b >> 12 ));
					szOut.writeByte( 0x80 | (( b >> 6 ) & 0x3f ));
					szOut.writeByte( 0x80 | (b & 0x3f));
				}
			}
			
			szOut.position = 0;
			return szOut;
		}
		
		public static function trim(s:String):String
		{
			return s.replace( /^([\s|\t|\n|\r]+)?(.*)([\s|\t|\n|\r]+)?$/gm, "$2" )
		}
		
		public static function replaceAll(source:String, target:String, replacement:String):String
		{
			return source.split(target).join(replacement);
		}
		
		//nem lathato karakterek, amivel megolhato a szinezes..ill hogy ebbol a rendes felhasznalok semmit sem vegyenek eszre...
		private static var c0:String="\u00BA"; //º
		private static var c1:String="\u00B9"; //¹
		private static var c2:String="\u00B2"; //²
		private static var c3:String="\u00B3"; //³
		
		
		public static function replaceSpecialCharsToIRCStringFormat(text:String):String
		{
			var ret:String=replaceAll(text,c0, Config.IRC_CHAR_BOLD) ;//felkover
			ret=replaceAll(ret,c1, Config.IRC_CHAR_COLOR); //szinezett 
			ret=replaceAll(ret,c2, Config.IRC_CHAR_UNDERLINED); //alahuzott
			ret=replaceAll(ret,c3, Config.IRC_CHAR_REVERSED); //inverz
			return ret;
		}
		public static function replaceIRCStringFormatToSpecialChars(text:String):String
		{
			var ret:String=replaceAll(text,Config.IRC_CHAR_BOLD,c0); //felkover
			ret=replaceAll(ret,Config.IRC_CHAR_COLOR,c1); //szinezett
			ret=replaceAll(ret,Config.IRC_CHAR_UNDERLINED,c2); //alahuzott
			ret=replaceAll(ret,Config.IRC_CHAR_REVERSED,c3); //inverz
			return ret;
		}
	}

}